//
// Created by 下次一定 on 2021/9/13.
//

#include<iostream>
#include <algorithm>

using namespace std;

const int N = 100010;
int n, m;
int h[N], size;


void down(int u) {
    int t = u;
    //小于左儿子
    if (u * 2 <= size && h[u * 2] < h[t]) t = u * 2;
    //小于右儿子
    if (u * 2 + 1 <= size && h[u * 2 + 1] < h[t]) t = u * 2 + 1;
    if (u != t) {
        swap(h[u], h[t]);
        down(t);
    }
}

void up(int u) {
    while (u / 2 && h[u / 2] > h[u]) {
        swap(h[u / 2], h[u]);
        u /= 2;
    }
}

int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> h[i];
    size = n;

    //如何构建小顶堆
    for (int i = n / 2; i; i--) down(i);
    while (m--) {
        cout << h[1];
        h[1] = h[size--];
        down(1);
    }
}